package com.woniuxy.educationplatform.controller;

import com.woniuxy.educationplatform.bean.GoodsBean;
import com.woniuxy.educationplatform.bean.ProjectBean;
import com.woniuxy.educationplatform.bean.ResultMsg;
import com.woniuxy.educationplatform.bean.TeacherBean;
import com.woniuxy.educationplatform.service.IGoodsService;
import com.woniuxy.educationplatform.service.IProjectService;
import com.woniuxy.educationplatform.service.ITeacherService;
import com.woniuxy.educationplatform.util.OrderUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private IGoodsService goodsServiceImpl;
    @Resource
    private ITeacherService teacherServiceImpl;
    @Resource
    private IProjectService projectServiceImpl;

    /**
     * 查询所有商品
     *
     * @return
     */
    @RequestMapping(value = "/page", produces = {"application/json;charset=utf-8"})
    public ResultMsg<GoodsBean> findAll() {
        List<GoodsBean> all = goodsServiceImpl.findAll();

        for (GoodsBean g : all) {
            ProjectBean projectBean = projectServiceImpl.findById(Long.valueOf(g.getFkProjectId()));
            g.setProjName(projectBean.getProjName());
            TeacherBean teacherBean = teacherServiceImpl.findById(Long.valueOf(g.getFkTeacherId()));
            g.setTeacherName(teacherBean.getTeacherName());
        }
        return new ResultMsg(200, "查询成功！", all);
    }

    /**
     * 查询一个
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/findById", produces = {"application/json;charset=utf-8"})
    public ResultMsg<GoodsBean> findById(@PathVariable("id") Integer id) {
        GoodsBean goodsBean = goodsServiceImpl.findById(id);
        return new ResultMsg(200, "查询一个商品！", goodsBean);
    }

    /**
     * 修改推荐
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{recommendId}", produces = {"application/json;charset=utf-8"})
    public ResultMsg updateRec(@PathVariable("recommendId") Integer id) {
        GoodsBean goodsBean = new GoodsBean();
        goodsBean.setId(id);
        goodsBean.setRecommend(0);
        goodsServiceImpl.updateRecommend(goodsBean);
        ResultMsg resultMsg = new ResultMsg(200, "修改", null);
        return resultMsg;
    }


    /**
     * 下架
     *
     * @param soid
     * @return
     */
    @GetMapping(value = "sold")
    public ResultMsg updateSold(Integer soid) {

        GoodsBean goodsBean = new GoodsBean();
        goodsBean.setId(soid);
        goodsBean.setStatus(1);
        goodsServiceImpl.updateStatus(goodsBean);
        ResultMsg resultMsg = new ResultMsg(200, "下架", null);
        return resultMsg;
    }

    /**
     * 取消推荐
     *
     * @param id
     * @return
     */
    @GetMapping(value = "outRecommend")
    public ResultMsg outRecommend(Integer id) {
        GoodsBean goodsBean = new GoodsBean();
        goodsBean.setId(id);
        goodsBean.setRecommend(1);
        goodsServiceImpl.updateRecommend(goodsBean);
        ResultMsg resultMsg = new ResultMsg(200, "取消推荐", null);
        return resultMsg;
    }


    /**
     * 上架
     *
     * @param soid
     * @return
     */
    @GetMapping(value = "putAway")
    public ResultMsg putAway(Integer soid) {

        GoodsBean goodsBean = new GoodsBean();
        goodsBean.setId(soid);
        goodsBean.setStatus(0);
        goodsServiceImpl.updateStatus(goodsBean);
        ResultMsg resultMsg = new ResultMsg(200, "上架成功", null);
        return resultMsg;
    }
    /**
     * 0--不删除
     * 1--删除
     * 弱删除
     *
     * @param delId
     * @return
     */
    @GetMapping(value = "del")
    public ResultMsg del(Integer delId) {
        goodsServiceImpl.deleteById(delId);
        ResultMsg resultMsg = new ResultMsg(200, "删除成功", null);
        return resultMsg;
    }
    /**
     * 模糊查询非实物商品
     *
     * @param goodsBean
     * @param createTime
     * @return
     * @throws ParseException
     */
    @GetMapping(value = "findLike")
    public ResultMsg find(GoodsBean goodsBean, String createTime) throws ParseException {
        if (createTime == "" | createTime == null) {
            System.out.println("没有查询时间");
        } else {
            createTime = createTime.replace("Z", " UTC");//是空格+UTC
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//格式化的表达式
            Date d = format.parse(createTime);
            goodsBean.setUploadTime(d);
        }

        System.out.println(goodsBean);
        List<GoodsBean> allByParams = goodsServiceImpl.findAllByParams(goodsBean);
        System.out.println(allByParams);
        ResultMsg resultMsg = new ResultMsg(200, "模糊查询", allByParams);
        return resultMsg;
    }
    /**
     * 模糊查询实物商品
     *
     * @param goodsBean
     * @param createTime
     * @return
     * @throws ParseException
     */
    @GetMapping(value = "findEntity")
    public ResultMsg findEntityParams(GoodsBean goodsBean, String createTime) throws ParseException {
        if (createTime == "" | createTime == null) {
            System.out.println("没有查询时间");
        } else {
            createTime = createTime.replace("Z", " UTC");//是空格+UTC
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//格式化的表达式
            Date d = format.parse(createTime);
            goodsBean.setUploadTime(d);
        }

        System.out.println(goodsBean);
        List<GoodsBean> allByParams = goodsServiceImpl.findEntityParams(goodsBean);
        System.out.println(allByParams);
        ResultMsg resultMsg = new ResultMsg(200, "模糊查询", allByParams);
        return resultMsg;
    }

    /**
     * 查询一个商品
     * @param id
     * @return
     */
    @GetMapping(value = "goodsMes")
    public ResultMsg goodsMes(Integer id) {
        GoodsBean goodsBean = goodsServiceImpl.findById(id);
        System.out.println(goodsBean);
        ResultMsg resultMsg = new ResultMsg(200, "查询一个商品信息", goodsBean);
        return resultMsg;
    }
    /**
     * 查找老师
     *
     * @return
     */
    @GetMapping(value = "teacher")
    public ResultMsg findTeacher() {
        List<TeacherBean> teacherBeans = teacherServiceImpl.findAll();
        System.out.println(teacherBeans);
        ResultMsg resultMsg = new ResultMsg(200, "查询老师成功", teacherBeans);
        return resultMsg;
    }

    /**
     * 添加一个商品
     *
     * @param goodsBean
     * @return
     */
    @GetMapping(value = "addGoods")
    public ResultMsg addGoods(GoodsBean goodsBean) {
        goodsBean.setGoodsNo(OrderUtil.generateOrderNo());
        int sales =  (int)((Math.random() *9+1)*100);
        goodsBean.setSales(sales);
        long counts = (long) ((Math.random() * 9 + 1) * 1000);
        goodsBean.setVisits(counts);
        System.out.println(goodsBean);
        goodsServiceImpl.saveGoods(goodsBean);
        ResultMsg resultMsg = new ResultMsg(200, "添加成功", null);
        return resultMsg;
    }

    /**
     * 添加实物
     *
     * @param goodsBean
     * @return
     */
    @GetMapping(value = "addEntity")
    public ResultMsg addEntity(GoodsBean goodsBean) {
        goodsBean.setGoodsNo(OrderUtil.generateOrderNo());
        System.out.println(goodsBean);
        goodsServiceImpl.saveEntity(goodsBean);
        ResultMsg resultMsg = new ResultMsg(200, "添加成功", null);
        return resultMsg;
    }

    /**
     * 修改实物
     *
     * @param goodsBean
     * @return
     */
    @GetMapping(value = "upEntity")
    public ResultMsg upEntity(GoodsBean goodsBean) {
        System.out.println(goodsBean);
        goodsServiceImpl.updateEntity(goodsBean);
        ResultMsg resultMsg = new ResultMsg(200, "修改成功", null);
        return resultMsg;
    }

    /**
     * 修改非实物
     *
     * @param goodsBean
     * @return
     */
    @GetMapping(value = "upGoods")
    public ResultMsg upGoods(GoodsBean goodsBean) {
        System.out.println(goodsBean);
        goodsServiceImpl.updateGoodsBean(goodsBean);
        ResultMsg resultMsg = new ResultMsg(200, "修改成功", null);
        return resultMsg;
    }

    /**
     * 查询一个实物
     *
     * @return
     */
    @RequestMapping(value = "/entity", produces = {"application/json;charset=utf-8"})
    public ResultMsg findEntity() {
        List<GoodsBean> all = goodsServiceImpl.findEntity();
        for (GoodsBean g : all
        ) {
            System.out.println(g);

        }
        return new ResultMsg(200, "查询成功！", all);
    }

    @RequestMapping(value = "/teaAndPro", produces = {"application/json;charset=utf-8"})
    public ResultMsg resultMsg(){
        List<TeacherBean> teacherBeans = teacherServiceImpl.findAll();
        return new ResultMsg(200, "查询成功！", teacherBeans);
    }

    @RequestMapping(value = "/teaAndPro1", produces = {"application/json;charset=utf-8"})
    public ResultMsg resultMsgPro(){
        List<ProjectBean> projectBeans = projectServiceImpl.findAll();
        return new ResultMsg(200, "查询成功！", projectBeans);
    }


}
